/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package popuplateDatabase;

import entities.Booking;
import entities.GuestUser;
import entities.Room;
import entities.RoomType;
import hotel.jpa.BookingJpaCtrl;
import hotel.jpa.IBookingJpaCtrl;
import hotel.jpa.IJpaCtrl;
import hotel.jpa.IRoomJpaCtrl;
import hotel.jpa.RoomJpaCtrl;
import hotel.jpa.RoomTypeJpaCtrl;
import hotel.jpa.UserJpaCtrl;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Anton Karlsson
 */
@WebServlet(name = "init", urlPatterns = {"/init"})
public class init extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
        

        EntityManagerFactory emf1 = Persistence.createEntityManagerFactory("hotel_pu");

        //Create new users and persist it. 
        IJpaCtrl userCtrl = new UserJpaCtrl(null, emf1);

        GuestUser guestUser = new GuestUser("guest", "guest@guest.com", "guest", 0);
        GuestUser adminUser = new GuestUser("admin", "admin@admin.com", "admin", 1);

        userCtrl.create(guestUser);
        userCtrl.create(adminUser);


        //Create new rooms and persist them 
        EntityManagerFactory emf2 = Persistence.createEntityManagerFactory("hotel_pu");

        IRoomJpaCtrl roomCtrl = new RoomJpaCtrl(null, emf2);
        IJpaCtrl roomTypeCtrl = new RoomTypeJpaCtrl(null, emf2);

        RoomType a1 = new RoomType(1, "Single room");
        Room a = new Room(1, true, true, 75, "Single");

        RoomType a2 = new RoomType(2, "Double room");
        Room b = new Room(3, true, true, 130, "Double");

        RoomType a3 = new RoomType(1, "Suite");
        Room c = new Room(4, true, true, 270, "Suite");
        
        roomTypeCtrl.create(a1);
        roomTypeCtrl.create(a2);
        roomTypeCtrl.create(a3);

        roomCtrl.create(a);
        roomCtrl.create(b);
        roomCtrl.create(c);


        //Boking; bookDate, startDate, endDate, room, user

        Booking b1 = new Booking(new Date(111, 9, 14), new Date(111, 9, 14), new Date(111, 9, 15), a, guestUser);

        //Create a booking
        EntityManagerFactory emf3 = Persistence.createEntityManagerFactory("hotel_pu");

        IBookingJpaCtrl bookingCtrl = new BookingJpaCtrl(null, emf3);

        bookingCtrl.create(b1);

        out.println("done");
        
        } finally {
            out.close();
        }
    }
   
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
